package com.database.bean;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.AttributeOverride;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

@Entity
@Table(name="PRODUCT")
@AttributeOverride( name="id", column = @Column(name="id") )
@XmlRootElement
public class Product extends BaseBean{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	
	@Size(min=1, max=50)
	@Column(name="productName")
	private String name=null;
	
	@Min(0)
	@Max(1)
	@Column(name="qualityFactor")
	private Double qualityFactor=null;
	
	@Min(0)
	@Max(1)
	@Column(name="imageFactor")
	private Double imageFactor=null;
	
	@Min(0)
	@Column(name="productCosts")
	private Double productCosts=null;
	
	@Column(name="active", columnDefinition = "BIT", length = 1)
	private boolean active;
	
	@ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name="supplierId")
	private Supplier supplier=null;
	
	@OneToMany(fetch = FetchType.LAZY, cascade={CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH})
	@Fetch(value = FetchMode.SUBSELECT)
    @JoinColumn(name="productId")
	private List<CompanyProduct>companyProducts =new ArrayList<CompanyProduct>();
	
	@Transient
	private Double possiblePrice=null;
	@Transient
	private Double possibleProfit=null;
	
	public Product(){}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the supplier
	 */
	public Supplier getSupplier() {
		return supplier;
	}

	/**
	 * @param supplier the supplier to set
	 */
	public void setSupplier(Supplier supplier) {
		this.supplier = supplier;
	}

	/**
	 * @return the qualityFactor
	 */
	public Double getQualityFactor() {
		return qualityFactor;
	}
	
	/**
	 * Helper method for website presentation with stars
	 * @return
	 */
	public int getQualityFactorStars(){
		int stars=0;
		Long lstars=(long) 0;
		
		if(this.getQualityFactor()!=null){
			lstars=Math.round(this.getQualityFactor()*10/2);
			stars=lstars.intValue();
		}
		
		return stars;
	}

	/**
	 * @param qualityFactor the qualityFactor to set
	 */
	public void setQualityFactor(Double qualityFactor) {
		this.qualityFactor = qualityFactor;
	}

	/**
	 * @return the imageFactor
	 */
	public Double getImageFactor() {
		return imageFactor;
	}

	/**
	 * @param imageFactor the imageFactor to set
	 */
	public void setImageFactor(Double imageFactor) {
		this.imageFactor = imageFactor;
	}

	/**
	 * @return the productCosts
	 */
	public Double getProductCosts() {
		return productCosts;
	}

	/**
	 * @param productCosts the productCosts to set
	 */
	public void setProductCosts(Double productCosts) {
		this.productCosts = productCosts;
	}

	@Override
	public String toString() {
		return "Product [supplier=" + supplier + ", name=" + name
				+ ", qualityFactor=" + qualityFactor + ", imageFactor="
				+ imageFactor + ", productCosts=" + productCosts + ", active="
				+ active + ", companyProducts=" + companyProducts + "]";
	}

	/**
	 * @return the companyProducts
	 */
	public List<CompanyProduct> getCompanyProducts() {
		return companyProducts;
	}

	/**
	 * @param companyProducts the companyProducts to set
	 */
	public void setCompanyProducts(List<CompanyProduct> companyProducts) {
		this.companyProducts = companyProducts;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	/**
	 * @return the possiblePrice
	 */
	public Double getPossiblePrice() {
		return possiblePrice;
	}

	/**
	 * @param possiblePrice the possiblePrice to set
	 */
	public void setPossiblePrice(Double possiblePrice) {
		this.possiblePrice = possiblePrice;
	}

	/**
	 * @return the possibleProfit
	 */
	public Double getPossibleProfit() {
		return possibleProfit;
	}

	/**
	 * @param possibleProfit the possibleProfit to set
	 */
	public void setPossibleProfit(Double possibleProfit) {
		this.possibleProfit = possibleProfit;
	}
	
}
